home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_005 / mandelbrotsrc / mand7.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  266 lines

  1.  
  2. /*****************************************************************************
  3.  *             MAND7.C - Save Dragon (Write Picture)
  4.  *           Mandelbrot Self-Squared Dragon Generator
  5.  *                  For the Commodore Amiga
  6.  *                       Version 2.01
  7.  *
  8.  *           Copyright (C) 1986, =Robert J. Mical=
  9.  *                Placed in the Public Domain
  10.  *
  11.  *  This program may be distributed free of charge as long as the above
  12.  *  notice is retained.  You may extract any or all information contained
  13.  *  in this file for use in your own programs
  14.  *
  15.  ****************************************************************************/
  16.  
  17. #include "mand.h"
  18.  
  19. extern struct Menu MainMenu[MENU_COUNT];
  20. extern struct MenuItem OptionsItems[OPTIONS_COUNT];
  21.  
  22. extern int MathBase, MathTransBase;
  23.  
  24. extern SHORT Color0, Color1, Color2;
  25. extern SHORT UserPalette[29];
  26.  
  27. /*----------------------*/
  28. /* Graphics definitions */
  29.  
  30. extern struct   GfxBase       *GfxBase;
  31. extern struct   IntuitionBase *IntuitionBase;
  32.  
  33. extern struct   RastPort      *rp,*rp2;
  34. extern struct   ViewPort      *vp;
  35.  
  36. extern struct   Window  *w,*w2, *ColorWindow;
  37. extern struct   Screen  *screen;
  38. extern struct   IntuiMessage  *message;
  39.  
  40. extern long last_color;
  41.  
  42. extern BOOL SettingCenter, SettingBoxSize;
  43.  
  44. /*----------------------------------*/
  45. /* Miscellaneous Global Definitions */
  46.  
  47. extern union kludge {
  48.    float f;
  49.    int i;
  50. } start_r,end_r,start_i,end_i;  /* Block bounds for set */
  51. extern int max_x,max_y,max_mem_y;  /* Graphics window size */
  52. extern int max_count,color_inc,color_offset,color_set,color_mode,color_div;
  53. extern int color_inset,func_num;
  54.  
  55. extern int v_starty,max_mem;
  56. extern long v_offset;
  57. extern UWORD *color_table,*v_mand_store;
  58.  
  59. extern int modified, want_read;
  60.  
  61. extern FILE *console,*v_fp,*redir_fp;
  62.  
  63. extern SHORT ZoomCenterX, ZoomCenterY, ZoomBoxSizeX, ZoomBoxSizeY;
  64. extern SHORT ZoomBoxStartX, ZoomBoxStartY;
  65.  
  66. extern struct NewScreen ns;
  67.  
  68. /* === variables defined here ============================================= */
  69.  
  70. UBYTE *FriendlyTools[] =
  71.     {
  72.     "FILETYPE=Graphicraft",
  73.     };
  74.  
  75. UWORD ProjectObjectData[] =
  76.     {
  77.     0x1FF0, 0x0000,
  78.     0x783C, 0x6000,
  79.     0xE00F, 0xF800,
  80.     0xE00F, 0xF800,
  81.     0x783C, 0x6000,
  82.     0x1FF0, 0x0000,
  83.     /* */
  84.     0x0000, 0x0000,
  85.     0x07C0, 0x0000,
  86.     0x1FF0, 0x0000,
  87.     0x1FF0, 0x0000,
  88.     0x07C0, 0x0000,
  89.     0x0000, 0x0000,
  90.     };
  91.  
  92. struct Image ProjectObjectImage = 
  93.     {
  94.     0, 0,
  95.     21, 6, 2,
  96.     &ProjectObjectData[0],
  97.     3, 0,
  98.     NULL,
  99.     };
  100.  
  101.  
  102. struct DiskObject ProjectObject =
  103.     {
  104.     WB_DISKMAGIC,
  105.     WB_DISKVERSION,
  106.         { 
  107.         /* the Gadget structure */
  108.         NULL,
  109.         0, 0,
  110.         25, 12,
  111.         GADGHBOX | GADGIMAGE,
  112.         RELVERIFY | GADGIMMEDIATE,
  113.         BOOLGADGET,
  114.         (APTR)&ProjectObjectImage,
  115.         NULL,
  116.         NULL,
  117.         0, 0, 0, 0,
  118.         },
  119.     WBPROJECT,
  120.     ":Graphicraft",
  121.     &FriendlyTools[0],
  122.     NO_ICON_POSITION, NO_ICON_POSITION,
  123.     NULL,
  124.     NULL,
  125.     PROCESS_STACKSIZE,
  126.     };
  127.  
  128.  
  129. LONG PictModes[3] =
  130.     {
  131.     CAMG,
  132.     4,
  133.     NULL,
  134.     };
  135.  
  136. LONG PictBody[2] =
  137.     {
  138.     BODY,
  139.     40000,
  140.     };
  141.  
  142.  
  143. struct PaintingHeader PaintingHeader =
  144.     {
  145.     FORM,               /* IFFID */
  146.     (40156),            /* file length (not a nice way (too bad, eh?)) */
  147.     ILBM,               
  148.     BMHD,               
  149.     sizeof(struct BitMapHeader),
  150.         {               /* start of the BitMapHeader structure */
  151.         320, 200,
  152.         0, 0,
  153.         5,
  154.         0, 
  155.         0,
  156.         0,
  157.         0,
  158.         10, 11,
  159.         320, 200,       /* end of BitMapHead */
  160.         },
  161.     CMAP,               
  162.     96,                 /* this value is followed by 32 color registers */
  163. };
  164.  
  165.  
  166.  
  167. BOOL SavePicture(filename)
  168. UBYTE *filename;
  169. {
  170.     SHORT i, j, offset;
  171.     ULONG length, actual_length, file;
  172.     UBYTE *p, component, rgbbyte;
  173.     USHORT rgb;
  174.     BOOL written;
  175.  
  176.     written = FALSE;
  177.  
  178.     /* Open the data file */
  179.     file = Open(filename, MODE_NEWFILE);
  180.     if (file == NULL)
  181.         /* Couldn't open as a new file */
  182.         goto EXIT_ERROR;
  183.  
  184.     /***********************************************************************
  185.      * to write the buffer to the disk, we need to write:
  186.      *      "FORM"
  187.      *      file length
  188.      *      "ILBM"
  189.      *      "BMHD"
  190.      *      BMHD length
  191.      *      BitMapHeader structure
  192.      *      "CMAP"
  193.      *      CMAP length
  194.      *      colors
  195.      *      pad byte if needed
  196.      *      "BODY"
  197.      *      BODY length
  198.      *      body data
  199.      **********************************************************************/
  200.  
  201.      /* First, write out the header */
  202.     length = sizeof(struct PaintingHeader);
  203.     actual_length = Write(file, &PaintingHeader, length);
  204.     if (actual_length != length)
  205.         goto WRITE_ERROR;
  206.  
  207.     /* also, enough bytes for each color register */
  208.     for (i = 0; i < (1 << ns.Depth); i++)
  209.         {
  210.         for (component = 0; component < 3; component++)
  211.             {
  212.             rgb = (GetRGB4(vp->ColorMap, i));
  213.             rgbbyte = (rgb >> (4 * (2 - component)));
  214.             rgbbyte <<= 4;
  215.             actual_length = Write(file, &rgbbyte, 1);
  216.             if (actual_length != 1)
  217.                 goto WRITE_ERROR;
  218.             }
  219.         }
  220.     actual_length = Write(file,  &PictBody[0], 8);
  221.     if (actual_length != 8)
  222.         goto WRITE_ERROR;
  223.  
  224.     /* Finally, the data (uncompressed for now). */
  225.     /* First, get the bytelength per line */
  226.     length = (((ns.Width + 15) >> 4) << 1);
  227.  
  228.     /* For every line ... */
  229.     for (j = 0; j < ns.Height; j++)
  230.         {
  231.         offset = length * j;
  232.         /* For every bit plane ... */
  233.         for (i = 0; i < ns.Depth; i++)
  234.             {
  235.             /* p is set to the start of the line in this bit plane */
  236.             p = (screen->BitMap.Planes[i]) + offset;
  237.             actual_length = Write(file, p, length);
  238.             if (actual_length != length) goto WRITE_ERROR;
  239.             }
  240.         }
  241.  
  242.     PictModes[2] = vp->Modes;
  243.     actual_length = Write(file,  &PictModes[0], 12);
  244.     if (actual_length != 12)
  245.         goto WRITE_ERROR;
  246.     if (PutDiskObject(filename, &ProjectObject) == FALSE)
  247.         goto WRITE_ERROR;
  248.  
  249.     written = TRUE;
  250.  
  251. WRITE_ERROR:
  252.     Close(file);
  253.  
  254. EXIT_ERROR:
  255.     if (NOT written)
  256.         {
  257.         fprintf(console, "*** COULDN'T WRITE YOUR FILE! ***\n");
  258.     DisplayBeep(NULL);
  259.         DeleteFile(filename);
  260.         }
  261.  
  262.     return(written);
  263. }
  264.  
  265.  
  266.